home *** CD-ROM | disk | FTP | other *** search
- <CFQUERY NAME="GetEmployees" DATASOURCE="cfsnippets">
- SELECT Emp_ID, FirstName, LastName, EMail, Phone, Department
- FROM Employees
- </CFQUERY>
-
- <!--- This example demonstrates the different kind
- of CFLOOP actions available --->
- <HTML>
-
- <HEAD>
-
- <TITLE>
- CFLOOP Example
- </TITLE>
- </HEAD>
-
- <BASEFONT FACE="Arial, Helvetica" SIZE=2>
- <BODY bgcolor="#FFFFD5">
-
- <H3>CFLOOP Example</H3>
-
- <P><B>Index loops</B>
- <BR>A simple loop: we can use an index parameter to
- tell us how many times we have looped through the expression.
- <P>
- <CFLOOP INDEX="counter" FROM="1" TO="5" STEP=1>
- <CFOUTPUT>We have cycled through the loop #counter# time<CFIF counter is not 1>s</CFIF><BR></CFOUTPUT>
- </CFLOOP>
-
- <P><B>Conditional loops</B>
- <BR>By stating a condition (tempVar is not 15), we can loop through
- and continue adding 3 onto tempVar; when tempVar is 15, the loop exits.
- <P>
- <CFPARAM name="tempVar" default=3>
- <CFLOOP CONDITION="tempVar is not 15">
-
- <CFSET tempVar = tempVar + 3>
- <CFOUTPUT>#TempVar#</CFOUTPUT>
- </CFLOOP>
-
- <P>Because tempVar now equals <CFOUTPUT>#tempVar#</CFOUTPUT>,
- the condition is fulfilled.
-
- <P><B>Looping over a query</B>
- <BR>Looping over a query can help us to display different
- output when a certain condition is reached (e.g. if the
- Department of the employee is "Engineering," display in red):
-
- <P>
- <CFLOOP QUERY="getEmployees">
- <CFOUTPUT>
- #FirstName# #LastName#,
- <CFIF #Department# is "Engineering">
- <font color=ff0000>#Department#</FONT>
- <CFELSE>#Department#
- </CFIF>
- <BR>
- </CFOUTPUT>
- </CFLOOP>
-
- <P><B>Looping over a list</B>
- <P>CF gives the ability to loop over a list, which we
- can create for this example by looping over a new query
- and creating a list of FirstName/LastName combinations.
- <P>While looping through our query, we can create the list
- variable, which we will then feed back to CFLOOP.
-
- <CFQUERY NAME="GetFirstLastName" DATASOURCE="cfsnippets">
- SELECT FirstName, LastName
- FROM Employees
- ORDER by LastName
- </CFQUERY>
-
- <!--- set up a temporary variable --->
- <CFSET tempList = "">
-
- <!--- populate the list by looping through the query --->
- <CFLOOP QUERY="GetFirstLastName">
- <CFSET tempList = tempList & "#FirstName# #LastName#,">
- </CFLOOP>
-
- <P>The list is as follows:
- <BR><CFOUTPUT>#tempList#</CFOUTPUT>
-
- <P>CFLOOP outputs the list:
- <BR>
- <!--- loop through the resulting list --->
- <CFLOOP INDEX="ListElement" LIST="#tempList#" DELIMITERS=",">
- <CFOUTPUT>#ListElement#</CFOUTPUT><BR>
- </CFLOOP>
-
-
- </BODY>
-
- </HTML>
-